package com.gmail.jaredjeya.awesomeplatformer.model;

import android.content.Context;
import android.os.Handler;

public class UpdateThread extends Thread {

	protected Game		game;
	protected Handler	handler;
	protected Context	context;
	private boolean		isSetup	= false, running = false;
	public static int	upsDraw;

	public UpdateThread(Game game, Handler handler, Context context) {
		super();
		this.handler = handler;
		this.game = game;
		this.context = context;
	}

	@Override
	public void run() {
		int ups = 0;
		long timer = System.currentTimeMillis();
		running = true;
		long lastTime = System.nanoTime();
		while (running) {

			if (!isSetup) {
				isSetup = true;
				game.setup(context);
			}

			if (System.currentTimeMillis() - timer >= 1000) {
				upsDraw = ups;
				ups = 0;
				timer = System.currentTimeMillis();
			}

			if (System.nanoTime() - lastTime >= 1000000000.0 / 60.0) {
				game.update();
				ups++;
				lastTime = System.nanoTime();
			}
		}
	}

}
